package com.zisuyi.uilibrary.module.dialog


import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import com.weilele.mvvm.activityList
import com.weilele.mvvm.base.helper.appCompatActivity
import com.weilele.mvvm.utils.activity.dip
import com.weilele.mvvm.utils.activity.getResString
import com.weilele.mvvm.utils.activity.textFrom
import com.weilele.mvvm.utils.safeGet
import com.zisuyi.uilibrary.R
import com.zisuyi.uilibrary.databinding.DialogSettingBinding
import com.zisuyi.uilibrary.module.activty.invite.InviteActivity
import com.zisuyi.uilibrary.module.dialog.rating.RatingDialog
import com.zisuyi.uilibrary.uitils.SoundUtils


class SettingDialog : BaseTitleDialog<DialogSettingBinding>() {
    companion object {
        fun show() {
            activityList.lastOrNull().safeGet<AppCompatActivity>()?.let {
                SettingDialog().show(it)
            }
        }
    }

    private var canMusic: Boolean
        set(value) {
            SoundUtils.isCanPlayBg = value
        }
        get() = SoundUtils.isCanPlayBg


    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        setTitle(getResString(R.string.settings))
        setMusicStatus()
    }

    override fun onRootViewLayoutParams(lp: FrameLayout.LayoutParams) {
        super.onRootViewLayoutParams(lp)
        lp.marginStart = dip(50)
        lp.marginEnd = lp.marginStart
    }

    override fun getClickView(): List<View?>? {
        return listOf(mBinding.tvRateUs, mBinding.tvInvite, mBinding.tvMusicSwitch, mBinding.tvExit)
    }

    override fun onSingleClick(view: View) {
        super.onSingleClick(view)
        when (view) {
            mBinding.tvRateUs -> {
                RatingDialog.show(appCompatActivity)
                dismiss()
            }
            mBinding.tvInvite -> {
                InviteActivity.start(appCompatActivity)
                dismiss()
            }
            mBinding.tvMusicSwitch -> {
                canMusic = !canMusic
                setMusicStatus()
            }
            mBinding.tvExit -> {
                dismiss()
                appCompatActivity?.onBackPressed()
            }
        }
    }

    private fun setMusicStatus() {
        if (!canMusic) {
            mBinding.tvMusicSwitch.setBackgroundResource(R.drawable.btn_bg_red)
            mBinding.tvMusicSwitch.textFrom(R.string.off)
        } else {
            mBinding.tvMusicSwitch.setBackgroundResource(R.drawable.btn_bg_grn)
            mBinding.tvMusicSwitch.textFrom(R.string.on)
        }
    }
}